package cn.mw.cmdb.virtualization.strategy.handler;

import cn.mw.cmdb.util.SeverityUtils;
import cn.mw.cmdb.virtualization.dto.VirtualizationDto;
import cn.mw.cmdb.virtualization.dto.VirtualizationHostDto;
import cn.mw.cmdb.virtualization.dto.VirtualizationVMDto;
import cn.mw.cmdb.virtualization.enums.VirtualizationVMStatusEnum;
import cn.mw.microMonitorCommon.entity.UnitResult;
import cn.mw.microMonitorCommon.enums.UnicloudTypeEnum;
import cn.mw.microMonitorCommon.enums.VcenterTypeEnum;
import cn.mw.microMonitorCommon.util.UnitUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author gengjb
 * @description 虚拟机类型DTO处理
 * @date 2024/7/1 14:35
 */
public class VMDtoHandler {

    public VirtualizationDto handler(List realDatas){
        VirtualizationDto dto = new VirtualizationDto();
        if(CollectionUtils.isEmpty(realDatas)){return dto;}
        for (Object realData : realDatas) {
            VirtualizationVMDto vmDto = (VirtualizationVMDto) realData;
            handleCpuValue(vmDto);
            handleMemoryValue(vmDto);
            handleSpaceValue(vmDto);
            handlerStatusValue(vmDto);
            handleCpuUsageRateValue(vmDto);
            handleMemoryUsageRateValue(vmDto);
            handlerUpdateTime(vmDto);
        }
        return dto;
    }

    private void handlerStatusValue(VirtualizationVMDto vmDto){
        String type = vmDto.getType();
        VcenterTypeEnum vcenterTypeEnum = VcenterTypeEnum.getVcenterTypeEnum(type);
        String status = vmDto.getStatus();
        if(vcenterTypeEnum != null && StringUtils.isNotEmpty(status)){
            VirtualizationVMStatusEnum vmStatusEnum = VirtualizationVMStatusEnum.getVMStatusEnum(status);
            if(vmStatusEnum != null){
                vmDto.setStatus(vmStatusEnum.getStatus());
            }
        }
    }

    private void handlerUpdateTime(VirtualizationVMDto vmDto){
        String runTime = vmDto.getRunTime();
        if(StringUtils.isNotEmpty(runTime) && !runTime.equals("-1")){
            String[] split = runTime.split("_");
            String lastTime = SeverityUtils.getLastTime(Long.parseLong(split[0]));
            vmDto.setRunTime(lastTime);
        }
    }

    private void handleCpuValue(VirtualizationVMDto vmDto){
        String usedCpu = vmDto.getUsedCpu();
        if(StringUtils.isNotEmpty(usedCpu)){
            String[] split = usedCpu.split("_");
            UnitResult result = UnitUtil.getConvertedValue(new BigDecimal(split[0]), split[1]);
            if(result != null){
                vmDto.setUsedCpu(result.getValue()+result.getUnits());
            }else{
                vmDto.setUsedCpu(split[0]+split[1]);
            }
        }
    }

    private void handleCpuUsageRateValue(VirtualizationVMDto vmDto){
        String cpuUsageRate = vmDto.getCpuUsageRate();
        if(StringUtils.isNotEmpty(cpuUsageRate)){
            String[] split = cpuUsageRate.split("_");
            UnitResult result = UnitUtil.getConvertedValue(new BigDecimal(split[0]), split[1]);
            if(result != null){
                vmDto.setCpuUsageRate(result.getValue()+result.getUnits());
            }else{
                vmDto.setCpuUsageRate(split[0]+split[1]);
            }
        }
    }

    private void handleMemoryUsageRateValue(VirtualizationVMDto vmDto){
        String memoryUsageRate = vmDto.getMemoryUsageRate();
        if(StringUtils.isNotEmpty(memoryUsageRate)){
            String[] split = memoryUsageRate.split("_");
            UnitResult result = UnitUtil.getConvertedValue(new BigDecimal(split[0]), split[1]);
            if(result != null){
                vmDto.setMemoryUsageRate(result.getValue()+result.getUnits());
            }else{
                vmDto.setMemoryUsageRate(split[0]+split[1]);
            }
        }
    }

    private void handleMemoryValue(VirtualizationVMDto vmDto){
        String usedMemory = vmDto.getUsedMemory();
        if(StringUtils.isNotEmpty(usedMemory)){
            String[] split = usedMemory.split("_");
            UnitResult result = UnitUtil.getConvertedValue(new BigDecimal(split[0]), split[1]);
            if(result != null){
                vmDto.setUsedMemory(result.getValue()+result.getUnits());
            }else{
                vmDto.setUsedMemory(split[0]+split[1]);
            }
        }
    }

    private void handleSpaceValue(VirtualizationVMDto vmDto){
        String usedSpace = vmDto.getUsedSpace();
        String freeSpace = vmDto.getFreeSpace();
        double tatalSpace = 0;
        String unit = null;
        if(StringUtils.isNotEmpty(usedSpace)){
            String[] split = usedSpace.split("_");
            tatalSpace += Double.parseDouble(split[0]);
            UnitResult result = UnitUtil.getConvertedValue(new BigDecimal(split[0]), split[1]);
            if(result != null){
                vmDto.setUsedSpace(result.getValue()+result.getUnits());
            }else{
                vmDto.setUsedSpace(split[0]+split[1]);
            }
            unit = split[1];
        }
        if(StringUtils.isNotEmpty(freeSpace)){
            String[] split = freeSpace.split("_");
            tatalSpace += Double.parseDouble(split[0]);
            UnitResult result = UnitUtil.getConvertedValue(new BigDecimal(split[0]), split[1]);
            if(result != null){
                vmDto.setFreeSpace(result.getValue()+result.getUnits());
            }else{
                vmDto.setFreeSpace(split[0]+split[1]);
            }
            unit = split[1];
        }
        if(StringUtils.isNotEmpty(vmDto.getPreparationSpace())){
            String[] split = vmDto.getPreparationSpace().split("_");
            tatalSpace = Double.parseDouble(split[0]);
            unit = split[1];
        }
        UnitResult result = UnitUtil.getConvertedValue(new BigDecimal(tatalSpace), unit);
        if(result != null){
            if(StringUtils.isNotEmpty(result.getUnits())){
                vmDto.setPreparationSpace(result.getValue()+result.getUnits());
            }else{
                vmDto.setPreparationSpace(result.getValue());
            }
        }else{
            vmDto.setPreparationSpace(tatalSpace+unit);
        }
    }
}
